/*
 * @Descripttion: 
 * @version: 
 * @Author: sueRimn
 * @Date: 2022-08-19 10:51:04
 * @LastEditors: sueRimn
 * @LastEditTime: 2022-09-19 15:52:37
 */
import { getToken, setToken } from "@/util/auth.js";
import NProgress from "nprogress";
import "nprogress/nprogress.css"; // 导入进度条样式，否则看不到效果
import Vue from 'vue';
import Router from 'vue-router';
import store from '../store';
import { constantRoutes } from "./constRoutes.js";
NProgress.configure({ showSpinner: false });
Vue.use(Router)
const createRouter = () => new Router({
  scrollBehavior(to, from, savedPosition) {
    return { top: 0 }
  },//始终滚动到顶部
  routes: constantRoutes
})
const router = createRouter();
store.dispatch("menu/setMenuList", constantRoutes);//固定路由 如果有动态路由可以
router.beforeEach((to, from, next) => {
  NProgress.start(); //开启进度条
  let hasToken = getToken();
  if (to.path == "/logout") {
    setToken(null, {});
    next({ path: "/login" })
  } else {
    if (hasToken) {
      if (to.path == "/login") {
        next({ path: "/" })
      } else {
        next();
      }
    } else {
      next()
    }
  }
  NProgress.done();
})
router.afterEach(() => {
  NProgress.done()
})
export default router;

